package com.chen.springboot.controller.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.springboot.common.AuthAccess;
import com.chen.springboot.common.PageResult;
import com.chen.springboot.common.Result;
import com.chen.springboot.dto.*;
import com.chen.springboot.entity.Gender;
import com.chen.springboot.entity.Role;
import com.chen.springboot.entity.User;
import com.chen.springboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 还有这里的@RestController因为是前后端分离项目，所以后端只用返回json数据就可以了，或者说对象数据。
 * 所以不能用@Controller，它会返回一个视图
 */
@Api(tags = {"用户接口"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @AuthAccess(message = "注册页面放行")
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {
        userService.register(user);
        return Result.success();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        UserDto userDto = userService.login(user);
        return Result.success(userDto);
    }

    @ApiOperation("管理员登录")
    @PostMapping("/manageLogin")
    public Result<?> manageLogin(@RequestBody User user) {
        UserDto userDto = userService.manageLogin(user);
        return Result.success(userDto);
    }

    @ApiOperation("新增用户")
    @PostMapping
//    public Result<?> save(@RequestBody User user) {
//        userService.save(user);
//        return Result.success();
//    }
    public   Result<?> saveUser(@RequestBody User user) {
        userService.saveUser(user);
        return Result.success();
    }


    @ApiOperation("更新用户信息")
    @PutMapping("/update")
    public Result<?> update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }
    @ApiOperation("更新用户密码")
    @PostMapping("/changePassword")
    public Result<?> changePassword(@RequestBody PasswordChangeDTO dto) {
        userService.changePassword(dto);
        return Result.success();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam Long id) {
        userService.delete(id);
        return Result.success();
    }

    @ApiOperation("批量删除用户")
    @PostMapping("/deleteList")
    public Result<?> deleteBatch(@RequestBody DeleteBatchDTO dto) {
        List<Integer> ids = dto.getId();
        userService.deleteBatch(ids);
        return Result.success("批量删除成功");
    }


    @ApiOperation("获取所有用户")
    @PostMapping("/list")
//    public Result<?> findPage(@RequestBody ReqPageUserDTO req) {
//        return Result.success(userService.findPage(req));
//    }
    public Result<Page<UserVO>> findPage(@RequestBody ReqPageUserDTO req) {
        Page<UserVO> page = userService.findPage(req);
        return Result.success(page);
    }

    @GetMapping("/all")
    public Result<PageResult<UserVO>> findAllUsers() {
        PageResult<UserVO> result = userService.findAllUsers();
        return Result.success(result);
    }



    @ApiOperation("获取性别")
    @GetMapping("/gender")
    public Result<List<Gender>> getGenderList() {
        List<Gender> genderList = userService.getGenderList();
        return Result.success(genderList);
    }

    /**
     * 获取用户身份列表
     */
    @ApiOperation("获取用户身份列表")
    @GetMapping("/role")
    public Result<List<Role>> getRoleList() {
        List<Role> identityList = userService.getRoleList();
        return Result.success(identityList);
    }





    @ApiOperation("重置密码")
    @PostMapping("/resetPassword")
    public Result<?> resetPassword(@RequestBody ResetPasswordDTO dto) {
        userService.resetPassword(dto);
        return Result.success("密码重置成功");
    }

    /**
     *
     */
    @PostMapping("/add")
    public Result<?> addUser(@RequestBody User user) {
        userService.addUserWithDefaultValues(user);
        return Result.success("新增用户成功");
    }

    //下载用户导入模板
    @ApiOperation("下载用户导入模板")
    @GetMapping("/template")
    public void downloadUserImportTemplate(HttpServletResponse response) {
        userService.downloadUserTemplate(response);
    }

    //导入用户数据
    @ApiOperation("用户导入excel")
    @PostMapping("/import")
    public Result<?> importUsers(@RequestParam("file") MultipartFile file,
                                 @RequestParam(value = "isCover", required = false) Boolean isCover) {
        try {
            userService.importUsersFromExcel(file, isCover != null && isCover);
            return Result.success("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("导入失败：" + e.getMessage());
        }
    }

    //导出用户数据
    @ApiOperation("导出用户数据")
    @GetMapping("/export")
    public void exportUserData(HttpServletResponse response) {
        userService.exportUserData(response);
    }

}